﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace RhythmComet.Services
{
    public interface IVibrationManager {}

    public class VibrationManager : GameComponent, IVibrationManager
    {
        int duration;
        static int instanceCount;

        public VibrationManager(RhythmCometGame game, int duration) : base(game)
        {
            this.duration = duration;
        }

        public void BeginVibration()
        {
            GamePad.SetVibration(PlayerIndex.One, 0.5f, 0.5f);
            Thread.Sleep(duration);
            lock (this)
            {
                if (VibrationManager.instanceCount == 1)
                {
                    GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f);
                }
                VibrationManager.instanceCount--;
            }
        }

        public void Start()
        {
            Thread thread;
            lock (this)
            {
                thread = new Thread(new ThreadStart(this.BeginVibration));
                VibrationManager.instanceCount++;
            }
            thread.Start();
        }
    }
}
